package com.administration.controller;

import com.administration.constants.Result;
import com.administration.dao.SysPostMapper;
import com.administration.pojo.PageInfo;
import com.administration.pojo.SysPost;
import com.administration.utils.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysPost")
public class SysPostController {

    @Resource
    private SysPostMapper sysPostMapper;

    //岗位信息展示主界面
    @RequestMapping("/post.do")
    public String postIndex(){
        return "/static/post/layui_post_index.html";
    }

    //查看信息
    @ResponseBody
    @RequestMapping(value = "/selectPost.do",method = RequestMethod.GET)
    public ResultData getAllPosts(SysPost sysPost, PageInfo pageInfo){
        Map<String,Object> map = new HashMap<>();
        map.put("postId",sysPost.getPostId());
        map.put("postCode",sysPost.getPostCode());
        map.put("postName",sysPost.getPostName());
        map.put("status",sysPost.getStatus());

        map.put("start",pageInfo.getStart());
        map.put("end",pageInfo.getEnd());

        List<SysPost> list = sysPostMapper.selectAllPost(map);

        for (SysPost sp:list){
            //修改状态显示
            if ("0".equals(sp.getStatus())){
                sp.setStatus("正常");
            }else {
                sp.setStatus("停用");
            }
        }
        return new ResultData<>(Result.SUCCESS.getCode(), "",list.size(), list);
    }

    //更新界面信息显示
    @ResponseBody
    @RequestMapping( "/getPost.do")
    public ResultData getPost(SysPost sysPost){
        Map<String,Object> map = new HashMap<>();
        map.put("postId",sysPost.getPostId());

        map.put("start",0);
        map.put("end",100);

        List<SysPost> list = sysPostMapper.selectAllPost(map);
        return new ResultData<>(Result.SUCCESS.getCode(), "",list.size(), list);
    }

    //新增信息
    @ResponseBody
    @RequestMapping(value = "/addPost.do",method = RequestMethod.POST)
    public int addPost(SysPost sysPost){
        //System.out.println(sysPost);
        int addPostRes = sysPostMapper.addPost(sysPost);
        System.out.println(addPostRes);
        return addPostRes;
    }

    //修改信息
    @ResponseBody
    @RequestMapping(value = "/updatePost.do",method = RequestMethod.POST)
    public int updatePost(SysPost sysPost){
        System.out.println(sysPost);
        int updatePost = sysPostMapper.updatePost(sysPost);
        System.out.println(updatePost);
        return updatePost;
    }

    //删除信息
    @RequestMapping(value = "/deletePost.do",method = RequestMethod.POST)
    @ResponseBody
    public int deletePost(SysPost sysPost){
        int deletePost = sysPostMapper.deletePost(sysPost);
        System.out.println(deletePost);
        return sysPostMapper.deletePost(sysPost);
    }

}
